using B.S.BaseData.Domain.Entities.Logs;
using B.S.BaseData.ErrorCode;
using B.S.BaseData.ErrorCode.ErrorCodes;
using B.S.BaseData.Infrastructure.Interfaces;
using B.S.DataBase.EFCoreServiers.Dtos.Depts;
using B.S.DataBase.EFCoreServiers.Dtos.Logs;
using B.S.DataBase.EFCoreServiers.IServices.Logs;
using B.S.DataBase.EFCoreServiers.Services.Depts;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System.Threading.Tasks;

namespace B.S.BaseData.ReadAPI.Controllers
{
    /// <summary>
    /// 操作日志管理
    /// </summary>
    [ApiController]
    [Route("api/[controller]/[action]")]
    [Authorize]
    [ApiExplorerSettings(GroupName = "OperationLog")]
    public class OperationLogController : ControllerBase
    {
        private readonly IOperationLogServices _logServices;
        private readonly IOperationLogRepository operationLog;

        public OperationLogController(IOperationLogServices logServices, IOperationLogRepository operationLog)
        {
            _logServices = logServices;
            this.operationLog = operationLog;
        }
        /// <summary>
        /// 获取操作日志列表
        /// </summary>
        /// <param name="logDto">查询条件</param>
        /// <returns>日志列表</returns>
        [HttpGet]
        public async Task<ApiResult<ApiPaging<GetOperationLogResponseDto>>> GetLogs([FromQuery] GetOperationLogDto logDto)
        {
            try
            {
                return await _logServices.GetOperationLogs(logDto);
            }
            catch (Exception ex)
            {
                return ApiResult<ApiPaging<GetOperationLogResponseDto>>.Fail("获取操作日志失败：" + ex.Message, StatusCodeEnum.Exception);
            }
        }

        /// <summary>
        /// 导出操作日志信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        public async Task<IActionResult> ExportOperationLog()
        {
            //获取数据
            var data = await operationLog.GetAll().ToListAsync();

            //调用
            var fileBytes = operationLog.ExportToExcelMy(data, "操作日志数据导出");


            string fileName = $"导出数据_{DateTime.Now:yyyyMMddHHmmss}.xlsx";

            return File(
                fileBytes,
                System.Net.Mime.MediaTypeNames.Application.Octet,
                fileName
            );
        }





    }
}